﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CommonFunc;

public partial class Manageweb_ManageLink_ManageImageLink : System.Web.UI.Page
{
    MODEL.Link ML = new MODEL.Link();
    MODEL.Link _ML = new MODEL.Link();

    BLL.Link BL = new BLL.Link();


    protected delegate void UploadThumbnail(FileUpload _FU, Label _LB, int Thumbwidth, string _Prefix);
    UploadThumbnail UploadThumb = new UploadThumbnail(AutoThumb.UploadThumbnail);


    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["MasterID"] == null)
        {
            Response.Redirect("../Default.aspx");
        }

        LB_ManageImgLink.Text = "";

        TABLE_Add_Mod.Visible = false;
        TABLE_ModImage.Visible = false;

        ClickInit();
    }


    #region 按钮事件注册 [初始化]
    protected void ClickInit()
    {
        try
        {
            if (LB_ID.Text == "")
            {
                Btn_Submit.Click += new EventHandler(Btn_Add_Click);

                IMG_LinkImage.Visible = false;

                FU_Image.Visible = true;

                RFV_FU_Image.Enabled = true;

                LB_TableTitle.Text = "添加链接";
            }
            else
            {
                Btn_Submit.Click += new EventHandler(Btn_Mod_Click);

                IMG_LinkImage.Visible = true;

                FU_Image.Visible = false;

                RFV_FU_Image.Enabled = false;

                LB_TableTitle.Text = "修改链接";
            }
        }
        catch (Exception ex)
        {
            LB_ManageImgLink.Text = ex.Message;
        }
    }
    #endregion


    #region 添加修改表格数据 [初始化]
    protected void TableDataInit()
    {
        try
        {
            if (LB_ID.Text == "")
            {
                TB_URL.Text = "";

                CB_State.Checked = true;
            }
            else
            {
                ML.Link_ID = Convert.ToInt32(LB_ID.Text);

                _ML = BL.GetDataByID(ML);

                IMG_LinkImage.ImageUrl = "~/UploadImage/" + _ML.Link_Image;

                TB_URL.Text = _ML.Link_URL;

                CB_State.Checked = _ML.Link_State;
            }
        }
        catch (Exception ex)
        {
            LB_ManageImgLink.Text = ex.Message;
        }
    }
    #endregion


    #region 链接图片 [格式化]
    public string FormatLinkImage(object _Image)
    {
        string LinkImage = Convert.ToString(_Image);

        return "~/UploadImage/" + LinkImage;
    }
    #endregion


    #region 链接状态 [格式化]
    protected string FormatState(object _State)
    {
        bool LinkState = Convert.ToBoolean(_State);

        if (LinkState)
        {
            return "已启用";
        }
        else
        {
            return "已锁定";
        }
    }
    #endregion


    #region 添加链接 [GridView] [Command]
    protected void LBtn_Add_Command(object sender, CommandEventArgs e)
    {
        try
        {
            TABLE_Add_Mod.Visible = true;

            LB_ID.Text = "";

            ClickInit();

            TableDataInit();
        }
        catch (Exception ex)
        {
            LB_ManageImgLink.Text = ex.Message;
        }
    }
    #endregion
    #region 修改链接 [GridView] [Command]
    protected void LBtn_Mod_Command(object sender, CommandEventArgs e)
    {
        try
        {
            TABLE_Add_Mod.Visible = true;

            TABLE_ModImage.Visible = true;

            LB_ID.Text = e.CommandArgument.ToString();

            ClickInit();

            TableDataInit();

            GV_ImgLinkList.DataBind();
        }
        catch (Exception ex)
        {
            LB_ManageImgLink.Text = ex.Message;
        }
    }
    #endregion
    #region 删除链接 [GridView] [Command]
    protected void LBtn_Del_Command(object sender, CommandEventArgs e)
    {
        try
        {
            ML.Link_ID = Convert.ToInt32(e.CommandArgument);

            BL.Del(ML);

            LB_ManageImgLink.Text = "删除成功";

            GV_ImgLinkList.DataBind();
        }
        catch (Exception ex)
        {
            LB_ManageImgLink.Text = ex.Message;
        }
    }
    #endregion
    #region 链接状态切换 [GridView] [Command]
    protected void Btn_State_Command(object sender, CommandEventArgs e)
    {
        try
        {
            ML.Link_ID = Convert.ToInt32(e.CommandArgument);

            BL.ModState(ML);

            GV_ImgLinkList.DataBind();
        }
        catch (Exception ex)
        {
            LB_ManageImgLink.Text = ex.Message;
        }
    }
    #endregion


    #region 添加链接 [Click]
    protected void Btn_Add_Click(object sender, EventArgs e)
    {
        try
        {
            UploadThumb(FU_Image, LB_Thumb, 150, "th_Link_");

            ML.Link_Title = "";
            ML.Link_Image = LB_Thumb.Text;
            ML.Link_URL = TB_URL.Text;
            ML.Link_State = CB_State.Checked;
            ML.Link_DateTime = DateTime.Now;

            BL.Add(ML);

            LB_ManageImgLink.Text = "添加成功";

            GV_ImgLinkList.DataBind();
        }
        catch (Exception ex)
        {
            LB_ManageImgLink.Text = ex.Message;
        }
    }
    #endregion


    #region 修改链接 [Click]
    protected void Btn_Mod_Click(object sender, EventArgs e)
    {
        try
        {
            ML.Link_Title = "";
            ML.Link_URL = TB_URL.Text;
            ML.Link_State = CB_State.Checked;
            ML.Link_ID = Convert.ToInt32(LB_ID.Text);

            BL.Mod(ML);

            LB_ManageImgLink.Text = "修改成功";

            GV_ImgLinkList.DataBind();
        }
        catch (Exception ex)
        {
            LB_ManageImgLink.Text = ex.Message;
        }
    }
    #endregion


    #region 修改链接图片 [Click]
    protected void Btn_ModImage_Click(object sender, EventArgs e)
    {
        try
        {
            UploadThumb(FU_ModImage, LB_Thumb, 150, "th_Link_");

            ML.Link_ID = Convert.ToInt32(LB_ID.Text);
            ML.Link_Image = LB_Thumb.Text;

            BL.ModLinkImage(ML);

            LB_ManageImgLink.Text = "修改链接图片";

            TableDataInit();

            GV_ImgLinkList.DataBind();
        }
        catch (Exception ex)
        {
            LB_ManageImgLink.Text = ex.Message;
        }
    }
    #endregion
    
}
